package cn.shyouchi.wuguanjia.android.ui.event.report;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Button;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.DataChangeEvent;
import cn.shyouchi.wuguanjia.android.utils.LogUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;

public class EventReportActivity extends BaseActivity implements ReportContract.View {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rv_report)
    RecyclerView mRvReport;
    @BindView(R.id.btn_add_report)
    Button mBtnAddReport;

    String projectId;

    List<ReportEntity.ResultMessageBean> reportEntities = new ArrayList<>();
    ReportPresenter mReportPresenter;
    ReportAdapter mReportAdapter;
    boolean needRefresh = true;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int initContentView() {
        return R.layout.activity_event_report;
    }

    @Override
    public void initInjector() {
        mReportPresenter = new ReportPresenter(mContext, this);
        projectId = getIntent().getStringExtra("projectId");

    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        mTopBar.setTitle(getTitle().toString());
        setSupportActionBar(mTopBar.getToolbar());
        EventBus.getDefault().register(this);
        mReportAdapter = new ReportAdapter(reportEntities, this);
        mRvReport.setLayoutManager(new LinearLayoutManager(mContext));
        mRvReport.setAdapter(mReportAdapter);




    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

    @OnClick(R.id.btn_add_report)
    public void onClick() {
        Intent intent = new Intent(mContext, AddEventReportActivity.class);
        intent.putExtra("projectId", projectId);
        startActivity(intent);
    }

    @Override
    public void onDataRefresh(List<ReportEntity.ResultMessageBean> resultMessages) {
        reportEntities.clear();
        reportEntities.addAll(resultMessages);
        mReportAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(ReportEntity.ResultMessageBean entity) {
        LogUtil.json(entity);
        Intent intent = new Intent(mContext, PreviewReportActivity.class);
        intent.putExtra("projectId", projectId);
        intent.putExtra("report", entity);
        startActivity(intent);
    }

    @Override
    public void onDelClick(ReportEntity.ResultMessageBean entity) {

    }


    @Override
    protected void onResume() {
        super.onResume();
        if (needRefresh) {
            mReportPresenter.getReportList(projectId);
            needRefresh = false;
        }
    }


    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void DataChange(DataChangeEvent event) {
        needRefresh = true;
    }
}
